import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;

public class Main {
    public static void main(String[] args) {
        System.out.println("Program is started!");

        try {
            File jpgFile = new File("/home/shelman/Share/Projects/IdeaProjects/TestJpeg/test2.jpg");
            Image imgSrc = javax.imageio.ImageIO.read(jpgFile);

            int imgWidth = imgSrc.getWidth(null);
            int imgHeight = imgSrc.getHeight(null);
            System.out.println("Image (width, height): （" + imgWidth + ", " + imgHeight + ").");

            BufferedImage imgTag = new BufferedImage(imgWidth / 2, imgHeight / 2, BufferedImage.TYPE_INT_RGB);
            imgTag.getGraphics().drawImage(imgSrc, 0, 0, imgWidth / 2, imgHeight / 2, null);

            FileOutputStream imgOut = new FileOutputStream("/home/shelman/Share/Projects/IdeaProjects/TestJpeg/new.jpg");
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(imgOut);
            encoder.encode(imgTag);
            imgOut.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return;
    }
}